package adapter;

import android.content.Context;
import android.database.Cursor;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;

public class CustomSimpleCursor extends SimpleCursorAdapter{
	Context context;
	Cursor mcCursor;
	DbAdapter db;
	public CustomSimpleCursor(Context context,  Cursor c, String[] from, int[] to) {
		super(context,com.necsv.R.layout.sqlite_entry, c, from, to);
		this.context = context;
		this.mcCursor = c;
		db = new DbAdapter(context);
		db.open();
		// TODO Auto-generated constructor stub
	}
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		if(convertView == null)
		{
			convertView = View.inflate(context,com.necsv.R.layout.sqlite_entry, null);
		}
		View view = convertView;
		mcCursor.moveToPosition(position);
		final HolderView holderView = new HolderView();
		holderView.txtId = (TextView) view.findViewById(com.necsv.R.id.id_entry);
		holderView.txtName = (TextView) view.findViewById(com.necsv.R.id.name_entry);
		holderView.txtAge = (TextView) view.findViewById(com.necsv.R.id.age_entry);
		holderView.btnDel = (Button) view.findViewById(com.necsv.R.id.btnDelEntry);
		
		holderView.txtId.setText(String.valueOf((mcCursor.getLong(0))));
		holderView.txtName.setText(mcCursor.getString(1));
		holderView.txtAge.setText(String.valueOf((mcCursor.getLong(2))));
		
		Button btn = holderView.btnDel;
		btn.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				// TODO Auto-generated method stub
				db.removeAt(holderView.txtName.getText().toString());
				
			}
		});
		return view;
	}
	private class HolderView
	{
		TextView txtId,txtName,txtAge;
		Button btnDel;
	}

}
